﻿@{
    ViewBag.Title = "关联产品";
    var customer = ViewBag.Customer as CubicFWNet.Data.Models.Customer;
    var proList = ViewBag.ProList as List<CubicFWNet.Data.ViewModal.ProductView>;
}


<section class="content-header">
    <h1>
        关联产品
    </h1>
    <ol class="breadcrumb">
        <li><a href="javascript:void(0);"><i class="fa fa-dashboard"></i>@Resources.Global.PageTitle</a></li>
        <li><a href="@Url.Action("Index","Home")">首页</a></li>
        <li class="active">关联产品</li>
    </ol>
</section>

<section class="content">
    <form class="form-horizontal" method="post" id="formMain">
        <div class="box box-solid">
            <div class="box-body">

                <input type="hidden" name="CustomerID" value="@customer.Id" />
                <input type="hidden" name="ProData" id="ProData" />

                <div class="row">
                    <div class="form-group col-md-6 col-sm-12">
                        <label for="CusName" class="col-sm-3 control-label">客户</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="CusName" name="CusName" readonly value="@customer.Name" />
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="form-group col-md-6 col-sm-12">
                        <label for="ProName" class="col-sm-3 control-label" onclick="GetProduct()"><span style="color:dodgerblue">搜索产品</span></label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="ProName" name="ProName" placeholder="请输入产品名称..." onkeyup="GetProduct()" />
                        </div>
                    </div>
                </div>
                <br />
                <br />
                <div id="divProList">
                    @if (proList != null)
                    {
                        foreach (var item in proList)
                        {
                            <div class="row panelLayout" id="@item.Id">
                                <div class="form-group col-md-5 col-sm-12">
                                    <label class="col-sm-3 control-label" onclick="ShowProModal('@item.Id')">内部名称</label>
                                    <div class="col-sm-9 ">
                                        <span class="form-control linkLayout inName" onclick="ShowProModal('@item.Id')">@item.Name</span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6 col-sm-12" style="display:none">
                                    <label class="col-sm-3 control-label">内部价格</label>
                                    <div class="col-sm-9 ">
                                        <span class="form-control txtLayout">@item.Price</span>
                                    </div>
                                </div>

                                <div class="form-group col-md-2 col-sm-12">
                                    <div class="col-sm-12">
                                        <span class="btn btn-primary btnCopy">复 制</span>&nbsp;&nbsp;
                                        <span class="btn btn-default btnClear">清 空</span>
                                    </div>
                                </div>

                                <div class="form-group col-md-5 col-sm-12">
                                    <label class="col-sm-3 control-label">客户使用名称</label>
                                    <div class="col-sm-7 ">
                                        <input type="text" class="form-control proName" id="CusName" name="CusName" value="@item.ProductByName" />
                                    </div>
                                </div>
                                <div class="form-group col-md-6 col-sm-12" style="display:none">
                                    <label class="col-sm-3 control-label">客户购买价格</label>
                                    <div class="col-sm-9 ">
                                        <input type="number" class="form-control proPrice" value="@item.PriceCustomer" readonly>
                                    </div>
                                </div>
                            </div>
                        }
                    }

                    <div class="row">
                        <div class="form-group col-md-6 col-sm-12">
                            <label for="Memo" class="col-sm-3 control-label">备注</label>
                            <div class="col-sm-9">
                                <textarea rows="3" class="form-control" id="Memo" name="Memo"></textarea>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="form-group col-md-6 col-sm-12">
                            <label for="Creator" class="col-sm-3 control-label">创建人</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="Creator" name="Tolerance" value="@ViewBag.RealName" readonly="readonly" />
                            </div>
                        </div>
                        <div class="form-group col-md-6 col-sm-12">
                            <label for="CreateTime" class="col-sm-3 control-label">创建时间</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="CreateTime" value="@DateTime.Now.ToString("yyyy-MM-dd HH:mm")" readonly="readonly" />
                            </div>
                        </div>
                    </div>

                </div>


                <div class="box-footer">
                    <div class="box-footer">
                        <span class="btn btn-primary " onclick="FormSub()">确 定</span>&nbsp;&nbsp;&nbsp;&nbsp;
                        <a class="btn btn-primary" href="@Url.Action("Index","Customer")">返 回</a>
                        <label class="danger">@ViewBag.Info</label>
                    </div>
                </div>
            </div>
        </div>
    </form>
</section>
@section Scripts {
    <script>
        //弹窗显示产品信息
        function ShowProModal(id) {
            var options = {
                id: null,
                title: '产品信息',
                width: "90%",
                height: "90%",
                url: "../Product/Info?PID=" + id,
                shade: 0.3,
                btn: ['返回'],
                btnclass: ['btn btn-danger'],
                callBack: null
            }
            $.modalOpen(options);
        }

        //提交表单
        function FormSub() {

            //获取产品列表
            var proList = $("#divProList .panelLayout");
            var prodata = [];
            var num = proList.size();

            for (i = 0; i < num; i++) {
                var id = proList.eq(i).attr("id");
                var name = $(".proName").eq(i).val().trim();
                var price = $(".proPrice").eq(i).val().trim();
                if (name != "") {
                    prodata.push({ ProductID: id, CustomerID: '@customer.Id', ProductByName: name, Price: price });
                }
            }

            $("#ProData").val(JSON.stringify(prodata));


            $("#formMain").submit();
        }

        //关联产品[复制产品名称]
        $("#divProList .btnCopy").click(function () {
            var num = $("#divProList .btnCopy").index(this);
            var name = $("#divProList .inName").eq(num).html();
            $("#divProList .proName").eq(num).val(name);
        })

        //取消关联
        $("#divProList .btnClear").click(function () {
            var num = $("#divProList .btnClear").index(this);
            $("#divProList .proName").eq(num).val("");
        })

        //搜索产品
        function GetProduct() {
            var name = $("#ProName").val().trim();

            var proList = $("#divProList .panelLayout");
            var num = proList.size();

            for (var i = 0; i < num; i++) {
                var inName = $("#divProList .inName").eq(i).html();
                if (inName.indexOf(name) >= 0) {
                    $("#divProList .panelLayout").eq(i).show();
                }
                else {
                    $("#divProList .panelLayout").eq(i).hide();
                }
            }
        }
    </script>}